package com.caifeng.myapplication.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.caifeng.myapplication.R;

public class DemoCal extends AppCompatActivity {

    TextView textView;
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;
    Button button6;
    Button button7;
    Button button8;
    Button button9;
    Button button0;
    Button add;//加
    Button cut;//减
    Button rid;//乘
    Button divide;//除
    Button result;
    Button point;
    Button clear;
    int pointCount = 0;
    int option = 0;//运算符状态
    boolean newdigital = true;//标记是否是新输入的数字
    boolean flag = true;//判断程序是否出错
    double a = 0, b = 0;//两个相加的数
    double sum = 0;
    double sumtype = 0;//判断输出的数是否有小数部分

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.cal_demo);
        textView = (TextView) findViewById(R.id.textView);
        button1 = (Button) findViewById(R.id.btn1);
        button2 = (Button) findViewById(R.id.btn2);
        button3 = (Button) findViewById(R.id.btn3);
        button4 = (Button) findViewById(R.id.btn4);
        button5 = (Button) findViewById(R.id.btn5);
        button6 = (Button) findViewById(R.id.btn6);
        button7 = (Button) findViewById(R.id.btn7);
        button8 = (Button) findViewById(R.id.btn8);
        button9 = (Button) findViewById(R.id.btn9);
        button0 = (Button) findViewById(R.id.btn0);
        add = (Button) findViewById(R.id.add);//+
        cut = (Button) findViewById(R.id.cut);//-
        rid = (Button) findViewById(R.id.rid);//*
        divide = (Button) findViewById(R.id.divide);//   /
        result = (Button) findViewById(R.id.btnResult);//=
        point = (Button) findViewById(R.id.btnPoint);// .
        clear = (Button) findViewById(R.id.clear);// AC

        button0.setOnClickListener(lisenter);
        button1.setOnClickListener(lisenter);
        button2.setOnClickListener(lisenter);
        button3.setOnClickListener(lisenter);
        button4.setOnClickListener(lisenter);
        button5.setOnClickListener(lisenter);
        button6.setOnClickListener(lisenter);
        button7.setOnClickListener(lisenter);
        button8.setOnClickListener(lisenter);
        button9.setOnClickListener(lisenter);
        add.setOnClickListener(lisenter);
        cut.setOnClickListener(lisenter);
        rid.setOnClickListener(lisenter);
        divide.setOnClickListener(lisenter);
        result.setOnClickListener(lisenter);
        point.setOnClickListener(lisenter);
        clear.setOnClickListener(lisenter);


    }

    View.OnClickListener lisenter = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView text = (TextView) findViewById(R.id.textView);
            String s = text.getText().toString();//获取文本框显示的字符串
            Button btn = (Button) v;
            //数字的输入
            if (btn.getId() == R.id.btn0 || btn.getId() == R.id.btn1
                    || btn.getId() == R.id.btn2 || btn.getId() == R.id.btn3
                    || btn.getId() == R.id.btn4 || btn.getId() == R.id.btn5
                    || btn.getId() == R.id.btn6 || btn.getId() == R.id.btn7
                    || btn.getId() == R.id.btn8 || btn.getId() == R.id.btn9
                    || (btn.getId() == R.id.btnPoint && pointCount == 0)) {
           if (btn.getId() == R.id.btnPoint) {
                    if (null == s || s.equals("")) {
                        s += "0" + btn.getText();
                    } else {
                        s += btn.getText();
                    }
                    pointCount = 1;
                } else {
                    s += btn.getText();
                }
                text.setText(s);

            }
            //运算符的输入
            if (btn.getId() == R.id.add || btn.getId() == R.id.divide
                    || btn.getId() == R.id.cut || btn.getId() == R.id.rid) {
                //如果已经有两个数，再按运算符就直接把结果运算出来保存到a中然后继续运算 1+1 再按 +
                if (null == s || s.equals("")) {
                    s = "0";
                }
                if (option != 0) {
                    b = Double.valueOf(s);
                    switch (option) {
                        case 1:
                            sum = a + b;
                            break;
                        case 2:
                            sum = a - b;
                            break;
                        default:
                            break;
                    }
                    a = sum;

                }
                if (option == 0) {
                    a = Double.valueOf(s);
                }
                switch (btn.getId()) {
                    case R.id.add:
                        option = 1;
                        break;
                    case R.id.cut:
                        option = 2;
                        break;
                    default:
                        break;
                }

                text.setText("");


            }
            //等于，运算结果
            if (btn.getId() == R.id.btnResult) {
                if (null == s || s.equals("")) {
                    s = "0";
                }
                b = Double.valueOf(s);
                switch (option) {
                    case 1:
                        sum = a + b;
                        break;
                    case 2:
                        sum = a - b;
                        break;
                    default:
                        break;
                }

                sumtype = sum % 1;
                if (sumtype > 0) {
                    pointCount = 1;
                }
                s = "" + sum;
                if (sumtype == 0) {
                    int end = (s.toString()).lastIndexOf(".");
                    String str = (s.toString()).substring(0, end);
                    s = "" + Integer.parseInt(str);
                    pointCount = 0;
                }
                if (flag) {
                    text.setText(s);
                }
                a = Double.valueOf(s);
                option = 0;
                flag = true;

            }
//            清除
            if (btn.getId() == R.id.clear) {
                text.setText("");
                pointCount = 0;
                option = 0;
                flag = true;
            }
        }
    };

}